package com.kern.rpcclient;

import com.alibaba.fastjson.JSONObject;
import com.kern.common.bean.Request;
import com.kern.common.bean.Response;

import java.io.*;
import java.net.Socket;

/**
 * Created by caoshi at 20:28 2021-07-24
 */
public class RPCClientTestMain {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Socket socket = new Socket("127.0.0.1", 8989);

        OutputStream out = socket.getOutputStream();
        ObjectOutputStream objOut = new ObjectOutputStream(out);

        // 封装请求, 设置目标方法的相关信息
        // 1. 所在的类全路径;
        // 2. 要调用的目标方法名
        Request request = new Request();
        request.setClassName("com.kern.rpcserver.service.impl.UserServiceImpl");
        request.setMethodName("getUserInfo");
        // 发送请求
        objOut.writeObject(request);

        socket.shutdownOutput();

        // 获取响应结果
        InputStream input = socket.getInputStream();
        ObjectInputStream objIn = new ObjectInputStream(input);
        Response res = (Response) objIn.readObject();
        JSONObject result = JSONObject.parseObject(res.getObj().toString());

        System.out.println(result);

        objOut.close();
        out.close();
        socket.close();

    }


}
